<?php
/**
 * Feed流服务类
 * @App	
 * @Group 
 * @Action FeedFlowService
 * @author lilei
 * @date 2018年9月3日
 *
 */
namespace app\common\service;
use app\common\library\CommonFun;
use app\common\model\article\PublishArticleCoinModel;
use app\common\model\article\PublishArticleModel;
use app\common\model\article\PublishWeiboModel;
use app\common\model\CommonModel;
use app\common\model\recommend\RecommendArticleEssenceModel;
use app\common\model\recommend\RecommendArticleTopModel;
use app\common\model\user\UserPairModel;
use app\common\redis\UserReadRedis;

class FeedFlowService {
	/**
	 * 用户标识
	 * @var string|int
	 */
	var $userMark;
	
	/**
	 * 是否是登录用户
	 * @var string
	 */
	var $isLogin=false;
	
	/**
	 * 每页item数量
	 * @var int
	 */
	var $pageSize=5;
	
	/**
	 * 开始时间戳
	 * @var int
	 */
	var $timeLineStart;
	
	/**
	 * 开始时间
	 * @var string
	 */
	var $timeLineStartDate;
	
	/**
	 * 结束时间戳
	 * @var int
	 */
	var $timeLineEnd;
	
	/**
	 * 结束时间
	 * @var string
	 */
	var $timeLineEndDate;
	
	/**
	 * 文章时间跨度
	 * @var integer
	 */
	var $timeLineOver = 86400;
	
	/**
	 * 用户已读文章列表
	 * @var array
	 */
	var $userReadArticleList = [];
	
	/**
	 * 用户已读微博列表
	 * @var array
	 */
	var $userReadWeiboList = [];
	
	var $topArticle = [];
	var $topWeibo = [];
	
	var $essenceArticle = [];
	var $essenceWeiBo = [];
	
	var $userAssetCoinIdList = [];
	var $userPairCoinIdList = [];
	
	var $userArticleList = [];
	var $userWeiBoList = [];
	
	
	/**
	 * 构造函数
	 * @param string $userMark
	 * @param string $isLogin
	 */
	public function __construct( $userMark, $isLogin ) {
		$this->userMark = $userMark;
		$this->isLogin = $isLogin;
		$this->__init();
	}
	
	/**
	 * 初始化
	 */
	private function __init() {
		// 文章时间跨度初始化
		$todayStartEndTime = CommonFun::getTodayStartEndTime();
		$this->timeLineEnd = $todayStartEndTime['end'];
		$this->timeLineStart =  $todayStartEndTime['start']-$this->timeLineOver;
		$this->timeLineEndDate = date("Y-m-d H:i:s", $this->timeLineEnd);
		$this->timeLineStartDate = date( "Y-m-d H:i:s", $this->timeLineStart );
		// 用户已读文章初始化
		$redisUserRead = UserReadRedis::getInstance();
		$this->userReadArticleList = $redisUserRead->getUserExposedArticleList($this->userMark, RecommendArticleTopModel::TypeArticle);
		$this->userReadWeiboList = $redisUserRead->getUserExposedArticleList($this->userMark, RecommendArticleTopModel::TypeWeiBo);
		// 置顶/加精 文章编号数据
		$this->getTopArticleList()->getEssenceArticleList();
		if ( $this->isLogin ) {
			$servicePython = PythonApiService::getInstance();
			$this->userAssetCoinIdList = $servicePython->getUserAssetCoinID($this->userMark);
			$modelUserPair = UserPairModel::getInstance();
			$this->userPairCoinIdList = $modelUserPair->get_user_pair_coin_id_list($this->userMark);
		}
	}
	
	/**
	 * 生成一页Feed流数据
	 */
	public function createFeedFlowPage() {
		
	}
	
	/**
	 * 获取用户相关文章列表
	 */
	public function getUserArticleList() {
		// 文章模型
		$modelPublishArticle = PublishArticleModel::getInstance();
		$modelWeiBo = PublishWeiboModel::getInstance();
		$modelArtCoin = PublishArticleCoinModel::getInstance();
		$coin_id_list = array_merge($this->userAssetCoinIdList, $this->userPairCoinIdList);
		// 币种相关文章列表
		if ( $coin_id_list ) {
			// 查找币种文章
			$art_coin_map = [
					"coin_id" => array('in', $coin_id_list),
					"article_id" => array('not in', $this->userReadArticleList),
			];
			$coin_art_list = $modelArtCoin->search_id_list_no_page($art_coin_map, 'article_id', false, 'create_time');
			if ( $coin_art_list ) {
				$coin_art_list = array_unique($coin_art_list);
				// 用户持仓币种文章
				$article_map = array(
						'id' => array('in', $coin_art_list),
						'is_published' => 1,
						'status' => 1,
						'deleted' => -1,
						'is_tmp' => -1,
						'create_time' => array('between', array( $this->timeLineStartDate, $this->timeLineEndDate )),
				);
				$res = $modelPublishArticle->search_id_list_no_page( $article_map, 'id', false, 'create_time' );
				$this->userArticleList = array_merge($this->userArticleList, $res?:array());
			}
            $_string = [];
			// 用户持仓币种微博
			$weibo_map = array(
					'status' => 3, // 已通过
					'publish_status' => 2, //已发布
					'id' => array('not in', $this->userReadWeiboList),
					'create_time' => array('between', array( $this->timeLineStartDate, $this->timeLineEndDate )),
			);
			foreach ( $coin_id_list as $coin_id ) {
				$_string[] = CommonModel::search_json_array($coin_id, 'coin_list');
			}
			$weibo_map['_string'] =implode(' or ', $_string);
			// 文章的相关微博列表
			$res = $modelWeiBo->search_id_list_no_page( $weibo_map, 'id', false, 'create_time' );
			$this->userWeiBoList =  array_merge($this->userWeiBoList, $res?:array());
		}
	}
	
	/**
	 * 置顶文章编号数据
	 * @return FeedFlowService
	 */
	public function getTopArticleList() {
		$modelTop = RecommendArticleTopModel::getInstance();
		// 获取置顶文章
		$topData = $modelTop->getAllTopDataByTime();
		if ( $topData ) {
			foreach ( $topData as $v ) {
				if ( $v['type'] == RecommendArticleTopModel::TypeArticle ) {
					if ( !in_array($v['article_id'], $this->userReadArticleList) ) {
						$this->topArticle[] = $v['article_id'];
					}
				}elseif ( $v['type'] == RecommendArticleTopModel::TypeWeiBo ) {
					if ( !in_array($v['article_id'], $this->userReadWeiboList) ) {
						$this->topWeibo[] = $v['article_id'];
					}
				}
			}
		}
		return $this;
	}
	
	/**
	 * 加精文章编号数据
	 * @return FeedFlowService
	 */
	public function getEssenceArticleList() {
		$modelEssence = RecommendArticleEssenceModel::getInstance();
		// 获取加精文章
		$essenceData = $modelEssence->getAllEssenceData();
		if ( $essenceData ) {
			foreach ( $essenceData as $v ) {
				if ( $v['type'] == RecommendArticleEssenceModel::TypeArticle ) {
					if ( !in_array($v['article_id'], $this->userReadArticleList) ) {
						$this->essenceArticle[] = $v['article_id'];
					}
				}elseif ( $v['type'] == RecommendArticleEssenceModel::TypeWeiBo ) {
					if ( !in_array($v['article_id'], $this->userReadWeiboList) ) {
						$this->essenceWeiBo[] = $v['article_id'];
					}
				}
			}
		}
		return $this;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}